#!/bin/bash

# 脚本目录
current_path=$(cd "$(dirname $0)";pwd)
# 脚本父级目录
base_path="$(dirname $current_path)"

# 判断~/.bash_profile是否存在
bash_profile_path="$HOME/.bash_profile"
if [ ! -f "$bash_profile_path" ]; then
    echo "创建 $bash_profile_path 文件"
    touch "$bash_profile_path"
fi

# 判断~/bin是否存在
if [ ! -d "$HOME/bin" ]; then
    echo "创建 $HOME/bin 文件夹"
    mkdir "$HOME/bin"
fi

# $1要拷贝的路径 $2命令文件路径 $3bash_profile文件配置内容
setup_command(){
    # 判断可执行文件是否存在
    if [ ! -f "$1" ]; then
        cp "$2" "$1"
    fi

    # ~/.bash_profile文件中是存在配置命令语句行
    grep "$1" "$bash_profile_path" > /dev/null
    if [ ! $? -eq 0 ]; then
        echo $3 >> "$bash_profile_path"
    fi
}

setup_command "$HOME/bin/PackageResources.exe" "$base_path/PackageResources/PackageResources/bin/Debug/PackageResources.exe" "export monoPackageRes=$HOME/bin/PackageResources.exe"

# 写入pack自定义命令
grep "alias pack=\"mono \$monoPackageRes\"" "$bash_profile_path" > /dev/null
if [ ! $? -eq 0 ]; then
    echo "alias pack=\"mono \$monoPackageRes\"" >> "$bash_profile_path"
fi

# 执行~/.bash_profile
source "$bash_profile_path"

echo "pack命令安装成功."
